package com.vlong.town.controller.admin.house;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.house.HouseInfo;
import com.vlong.town.entity.user.User;
import com.vlong.town.entity.wxUser.WxUser;
import com.vlong.town.service.house.HouseInfoService;
import com.vlong.town.service.user.UserService;
import com.vlong.town.service.wxUser.WxUserService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 房屋出租信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/house")
public class HouseInfoController extends BaseController {

    // Fields

    @Autowired
    private HouseInfoService houseInfoService;// 房屋出租信息业务实现类
    @Autowired
    private UserService userService;//web端用户实现类
    @Autowired
    private WxUserService wxUserService;//微信端用户实现类
    
    /**
     * 进入房屋出租信息列表页面
     * @return house/list_house
     */
    @RequestMapping("/list")
    public String gotoHouseInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc,
            @RequestParam(required=false, value="status") String status
            ) {
    	Integer  s=null;
    	if(status!=null&&!status.equals("-1")){
    		s =  Integer.parseInt(status);
    	}
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<HouseInfo> houseInfoList = this.houseInfoService.queryList(title, content, desc,s);
        PageInfo<HouseInfo> pageInfo = new PageInfo<HouseInfo>(houseInfoList);
        PageTag<HouseInfo> pt = new PageTag<HouseInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        request.setAttribute("status", status);
        for(HouseInfo houseInfo : houseInfoList) {
            houseInfo.setContent(DataUtil.delHTMLTag(houseInfo.getContent(), 0));
            houseInfo.setMonthlyRent(houseInfo.getMonthlyRent() / 1000);
            String publicUser = houseInfo.getPublicUser();
            if(null!=publicUser&&""!=publicUser){
            	//判断publicUser是否为纯数字
            	boolean flag = StringUtils.isNumeric(publicUser);
            	if(flag==true){//查询User
            		User user = userService.queryById(Long.parseLong(publicUser));
            		if(user.getRealName()!=null&&user.getRealName()!=""){
            			houseInfo.setUserName(user.getRealName());
            		}else{
            			houseInfo.setUserName(user.getUserName());
            		}
            	}else{//查询wxUser
            		WxUser  wxUser = wxUserService.queryByWxId(publicUser);
            		if(wxUser.getRealName()!=null&&wxUser.getRealName()!=""){
            			houseInfo.setUserName(wxUser.getRealName());
            		}else{
            			houseInfo.setUserName(wxUser.getWxName());
            		}
            	}
            }
        }
        request.setAttribute("houseInfoList", houseInfoList);
        return "admin/house/list_house";
    }

    
    
    
    /**
     * 进入房屋出租信息添加页面
     * @return house/add_house
     */
    @GetMapping("/addHouse")
    public String gotoHouseInofAddPage() {
        return "admin/house/add_house";
    }

    /**
     * 进入房屋出租信息修改页面
     * @return house/edit_house
     */
    @GetMapping("/editHouse/{infoId}")
    public String gotoHouseInofEditPage(@PathVariable(required=true, value="infoId") Long infoId) {
        HouseInfo houseInfo = this.houseInfoService.queryById(infoId);
        houseInfo.setMonthlyRent(houseInfo.getMonthlyRent() / 1000);
        request.setAttribute("houseInfo", houseInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + houseInfo.getThumbnail());
        return "admin/house/edit_house";
    }

    /**
     * 添加房屋出租信息
     * @param title 房屋出租标题
     * @param acreage 房屋面积
     * @param address 房屋位置
     * @param monthlyRent 月租金额
     * @param content 房屋出租内容
     * @param desc 房屋出租简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddHouse")
    public DataMessage addHouse(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="acreage", required=true) String acreage,
            @RequestParam(name="address", required=true) String address,
            @RequestParam(name="monthlyRent", required=true) String monthlyRent,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("房屋招商信息标题不合法");
            return dm;
        }
        this.houseInfoService.add(1L, title, acreage, address, Long.parseLong(monthlyRent) * 1000, content, desc, picture,getUserId().toString());
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("房屋招商信息添加成功");
        return dm;
    }

    /**
     * 修改房屋出租信息
     * @param title 房屋出租标题
     * @param acreage 房屋面积
     * @param address 房屋位置
     * @param monthlyRent 月租金额
     * @param content 房屋出租内容
     * @param desc 房屋出租简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditHouse")
    public DataMessage editHouse(
            @RequestParam(name="infoId", required=true) Long infoId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="acreage", required=true) String acreage,
            @RequestParam(name="address", required=true) String address,
            @RequestParam(name="monthlyRent", required=true) String monthlyRent,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("房屋招商信息标题不合法");
            return dm;
        }
        this.houseInfoService.update(infoId, title, acreage, address, Long.parseLong(monthlyRent) * 1000, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("房屋招商信息修改成功");
        return dm;
    }

    /**
     * 删除房屋出租
     * @param infoId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="infoId") Long infoId) {
        DataMessage dm = new DataMessage();
        if(infoId != null && infoId > 0L) {
            this.houseInfoService.delete(infoId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("房屋出租删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除房屋出租
     * @param infoIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="infoIds") String infoIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(infoIds, 1, 255)) {
            String[] infoIdArray = infoIds.split(",");
            for(String infoId : infoIdArray) {
                this.houseInfoService.delete(Long.parseLong(infoId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("房屋出租删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    
    /**
     * 审核房屋出租
     * @param infoId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @RequestMapping("/shenHeHouse")
    public DataMessage shenHeHouse(@RequestParam(required=true, value="infoId") Long infoId) {
        DataMessage dm = new DataMessage();
        if(infoId != null && infoId > 0L) {
            this.houseInfoService.updateStatus(infoId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("审核成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
}
